package com.kefu.robot.controller;

import java.util.List;

import com.kefu.chat.model.ChatChannel;
import com.kefu.chat.service.ChatChannelService;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.DeleteVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DetailVo;
import com.kefu.robot.model.RobotInfo;
import com.kefu.robot.service.RobotInfoService;
import com.kefu.robot.vo.RobotInfoInsertVo;
import com.kefu.robot.vo.RobotInfoSearchVo;
import com.kefu.robot.vo.RobotInfoUpdateVo;
import com.kefu.sys.mapper.SysTenantMapper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "机器人")
@RestController
@RequestMapping("robot/info")
public class RobotInfoController extends AbstractBaseController {
    @Autowired
    private ChatChannelService channelService;
    @Autowired
    private SysTenantMapper sysTenantMapper;
    @Autowired
    private RobotInfoService robotInfoService;

    @PostMapping("/add")
    @ApiOperation(value = "新增机器人", notes = "新增机器人")
    public Result<String> add(@Validated @RequestBody RobotInfoInsertVo robotInfoVo) {
        RobotInfo info = robotInfoVo.convert();
        String tenantId = KefuContext.getKefuContext().getTenantId();
        if(robotInfoService.count(Wrappers.lambdaQuery(RobotInfo.class).eq(RobotInfo::getTenantId,tenantId)) >= sysTenantMapper.selectById(tenantId).getRobotCount()){
            throw BusinessException.msg("数量已超上限");
        }
        info.setRobotNo(robotInfoService.getMaxRobot(KefuContext.getKefuContext().getTenantId()) + 1);
        robotInfoService.save(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改机器人", notes = "如果禁用机器人robotStatus传值0")
    public Result<String> update(@Validated @RequestBody RobotInfoUpdateVo robotInfoVo) {
        logger.info("修改机器人:{}", robotInfoVo);
        RobotInfo info = robotInfoVo.convert();
        RobotInfo dbInfo = robotInfoService.findById(robotInfoVo.getId());
        info.setTenantId(dbInfo.getTenantId());
        info.setRobotNo(dbInfo.getRobotNo());
        robotInfoService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "机器人详情", notes = "机器人详情")
    public Result<RobotInfo> detail(@Validated @RequestBody DetailVo detail) {
        RobotInfo robotInfo = robotInfoService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(robotInfo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询机器人", notes = "查询机器人，返回所有启用的机器人")
    public Result<List<RobotInfo>> list() {
        List<RobotInfo> list = robotInfoService.list(Wrappers.lambdaQuery(RobotInfo.class).eq(RobotInfo::getTenantId,KefuContext.getKefuContext().getTenantId())
                .eq(RobotInfo::getRobotStatus,1).orderByAsc(RobotInfo::getRobotNo));
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/listPage")
    @ApiOperation(value = "查询机器人分页", notes = "查询机器人分页")
    public Result<Page<RobotInfo>> listPage(@Validated @RequestBody RobotInfoSearchVo search) {
    	Page<RobotInfo> list = robotInfoService.search(search);
        return ResultGenerator.genSuccessResult(list);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "机器人删除", notes = "机器人删除")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("机器人删除:{},{}", delete, KefuContext.getTid());
        RobotInfo robotInfo = robotInfoService.findById(delete.getId());
        Assert.isNull(robotInfo, "机器人已经被删除！");
        Integer robotNo = robotInfo.getRobotNo();
        List<ChatChannel> list = channelService.list(Wrappers.lambdaQuery(ChatChannel.class).eq(ChatChannel::getRobotNo, robotNo));
        Assert.isNotEmpty(list, "机器人已经被使用，不能删除！");
        robotInfoService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

}
